﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PACS.Data;
using System.Data;
using WoSoft.PACS.Data.Models;

namespace WoSoft.PACS.Data.DepartmentDoctor
{
    /// <summary>
    /// 部门人员操作数据
    /// </summary>
    public class DepartmentDoctorData : Db
    {
        //获取所有部门数据
        public DataTable GetAllList()
        {
            return this.SqlDatabase.Sql("select * from 部门", null).QuerySingle<DataTable>();
        }

        //获取所有操作员数据
        public DataTable GetUserAllList()
        {
            return this.SqlDatabase.Sql("select * from 系统用户", null).QuerySingle<DataTable>();
        }

        //获取部门数据
        public DepartmentModel GetDepartmentByID(string ID)
        {
            var sql = "select * from 部门 where ID = @id";
            var dyobj = this.SqlDatabase.Sql(sql, null)
                .Parameter("id", ID)
                .QuerySingle<dynamic>();
            if (dyobj != null)
            {
                return new DepartmentModel()
                {
                    ID = dyobj.ID,
                    Name = dyobj.简称,
                    Code = dyobj.简码,
                    No = dyobj.编码,
                    ParentID = dyobj.上级ID,
                    Tel = dyobj.电话,
                    Remark = dyobj.备注
                };
            }

            return null;
        }


        //增加原型
        public int Insert(DepartmentModel model)
        {
            return this.SqlDatabase.Insert("部门")
                .Column("ID", model.ID)
                .Column("简称", model.Name)
                .Column("简码", model.Code)
                .Column("编码", model.No)
                .Column("电话", model.Tel)
                .Column("备注", model.Remark)
                .Column("上级ID", model.ParentID)
                .Execute();
        }

        //修改原型
        public int Update(DepartmentModel model)
        {
            return this.SqlDatabase.Update("部门")
                .Column("简称", model.Name)
                .Column("简码", model.Code)
                .Column("编码", model.No)
                .Column("电话", model.Tel)
                .Column("备注", model.Remark)
                .Column("上级ID", model.ParentID)
                .Where("ID",model.ID)
                .Execute();
        }

        public DataTable GetDataByDepartmentID(string departmentID)
        {
            var sql = "select * from 医护人员 where 部门id=@部门id";
            return this.SqlDatabase.Sql(sql).Parameter("部门id",departmentID).QuerySingle<DataTable>();
        }

        /// <summary>
        /// 根据部门ID获取部门名称
        /// </summary>
        /// <param name="departid"></param>
        /// <returns></returns>
        public string GetDepartNameById(string departid)
        {
            var sql = "select 简称 from 部门 where ID = @部门ID";
            return this.SqlDatabase.Sql(sql).Parameter("部门ID", departid).QuerySingle<string>();
        }

        //删除部门
        public void DeleteDepartment(string departmentID)
        {
            this.SqlDatabase.Delete("部门")
                .Where("ID", departmentID)
                .Execute();
        }
    }
}
